package rs.dalao.syc.user.all.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Service;
import rs.dalao.syc.common.pojo.authentication.CurrentPrincipal;
import rs.dalao.syc.user.all.dao.persist.repository.IUserPersonalRepository;
import rs.dalao.syc.user.all.pojo.entity.UserPersonal;
import rs.dalao.syc.user.all.pojo.param.UserPersonalAddInfo;
import rs.dalao.syc.user.all.pojo.param.UserUpdateInfo;
import rs.dalao.syc.user.all.pojo.vo.UserPersonalListVO;
import rs.dalao.syc.user.all.service.IUserPersonalService;

@Service
public class UserPersonalServiceImpl implements IUserPersonalService {
    @Autowired
    private IUserPersonalRepository iUserPersonalRepository;

    //根据id查询用户的真实信息，在前端进行展示
    @Override
    public UserPersonalListVO selectInfoById(Long id) {
        return iUserPersonalRepository.selectInfoById(id);
    }

    //根据用户id判断用户的真实信息是否存在
    //添加用户真实信息
    @Override
    public int insert(UserPersonalAddInfo userPersonalAddInfo, Long id) {

        UserPersonal userPersonal = new UserPersonal();
        userPersonal.setUserId(id);
        BeanUtils.copyProperties(userPersonalAddInfo, userPersonal);

        //根据用户id判断用户的真实信息是否存在
        UserPersonalListVO userPersonalListVO = iUserPersonalRepository.selectInfoById(id);
        //如果信息不存在，执行新增
        if (userPersonalListVO == null) {
            int insert = iUserPersonalRepository.insert(userPersonal);
            //执行新增后，将syc_user中的role从1变成2
            iUserPersonalRepository.updateRoleById(id);
            return insert;
        }

        userPersonal.setUserId(id);
        //如果信息存在，修改用户真实信息
        return iUserPersonalRepository.updateByUserId(userPersonal);
    }

/*    @Override
    public int update(UserUpdateInfo updateInfo,Long id) {
        UserPersonal userPersonal=new UserPersonal();
        BeanUtils.copyProperties(updateInfo,userPersonal);
        userPersonal.setUserId(id);
        return iUserPersonalRepository.updateByUserId(userPersonal);
    }*/
}
